#include "StompedReality/Asset/PatchSystem.h"
namespace StompedReality
{
namespace Asset
{
float* PatchSystem::CalculateBufferRect (
  int width, int height )
{
  // left = 0;
  // right = w;
  // bottom = h;
  // top = 0
  float* buffer = new float[16];

  // left bottom
  buffer[0] = -width * 0.5f;
  buffer[1] = height * 0.5f;

  // right bottom
  buffer[4] = width * 0.5f;
  buffer[5] = height * 0.5f;

  // left top
  buffer[8] = -width * 0.5f;
  buffer[9] = -height * 0.5f;

  // right top
  buffer[12] = width * 0.5f;
  buffer[13] = -height * 0.5f;

  // return buffer filled with pos
  return buffer;
}

void PatchSystem::UpdateUV ( float* buffer,
                             int textureWidth, int textureHeight,
                             int offsetX, int offsetY,
                             int width, int height )
{
  // left = offsetX / tw
  // right = offsetX + w / tw
  // top = offsetY / tw
  // top = offsetY + h / tw
  // left bottom
  buffer[2] = ( float ) offsetX / ( float ) textureWidth;
  buffer[3] = ( float ) ( offsetY + height ) / ( float ) textureHeight;

  // right bottom
  buffer[6] = ( float ) ( offsetX + width ) / ( float ) textureWidth;
  buffer[7] = ( float ) ( offsetY + height ) / ( float ) textureHeight;

  // left top
  buffer[10] =  ( float ) offsetX / ( float ) textureWidth;
  buffer[11] = ( float ) offsetY / ( float ) textureHeight;

  // right top
  buffer[14] = ( float ) ( offsetX + width ) / ( float ) textureWidth;
  buffer[15] = ( float ) offsetY / ( float ) textureHeight;
}

}
}
